<?php
/**
 * ClassName:渠道详情数组获取器
 * @auditor:leijunxiang
 * @time 2025/4/15 15:21
 */

namespace app\tool\freight;

use app\model\freight\ChannelDetailTemporary;
use app\modelOperate\freightForward\ChannelDetailOpe;
use app\modelOperate\freightForward\ChannelOpe;
use app\modelOperate\freightForward\WarehousePriceOpe;
use app\tool\ToolBase;

class ChannelDetailAttrHandle extends ToolBase
{
    //渠道详情数组
    protected $channelDetailAttr;

    /**
     * (渠道详情数组)开始处理(货代数组,仓库名称,当前重量)
     */
    public function startHandle($forwardArr, $warehouseName, $currentWeight)
    {
        if (empty($forwardArr) || empty($warehouseName) || empty($currentWeight)) {
            $this->errCode = FAIL;
            $this->errMsg = '参数错误';
            return [];
        }
        $arr = [];
        //初始化数据
        $this->initData();
        foreach ($forwardArr as $forward) {
            //获取渠道列表
            $channelList = $this->getChannelList($forward['forwardId']);
            if ($channelList) {
                foreach ($channelList as $channelData) {
                    //获取渠道详情(渠道id，仓库名称)
                    $data = $this->getChannelDetail($channelData['channelId'], $warehouseName);
                    if (empty($data)) {
                        continue;
                    }
                    $channelDetail = $data[0];
                    $obj = new ChannelDetailTemporary();
                    //货代id
                    $obj->setForwardId($forward['forwardId']);
                    //货代名称
                    $name = $forward['name'];
                    $obj->setName($name);
                    //获取仓库价格数组(详情id)
                    $priceAttr = $this->getWarehousePriceAttr($channelDetail['detailId']);
                    //区域名称
                    $areaName = $channelDetail['areaName'];
                    $obj->setAreaName($areaName);
                    //渠道id
                    $channelId = $channelData['channelId'];
                    $obj->setChannelId($channelId);
                    //渠道名称
                    $channelName = $channelData['channelName'];
                    $obj->setChannelName($channelName);
                    //仓库价格数组
                    $obj->setWarehousePrice($priceAttr);
                    $this->channelDetailAttr[] = $obj;
                }
            }
        }
        $arr = $this->channelDetailAttr;
        //清楚数据
        $this->cleanData();
        return $arr;
    }

    /**
     * 初始化数据
     */
    protected function initData()
    {
        $this->channelDetailAttr = [];
    }


    /**
     * 获取渠道列表(货代id)
     */
    protected function getChannelList($forwardId)
    {
        $channelOpe = app(ChannelOpe::class);
        //根据货代id查询渠道数据(货代id)
        $channelList = $channelOpe->selectChannelDataByForwardId($forwardId);

        return $channelList;
    }

    /**
     * 获取渠道详情(渠道id)
     */
    protected function getChannelDetail($channelId, $warehouseName)
    {
        $ope = app(ChannelDetailOpe::class);
        //根据id查询数据
        $detailAttr = $ope->selectDetailDataByChannelId($channelId, $warehouseName);
        return $detailAttr;
    }

    /**
     * 清楚数据
     */
    protected function cleanData()
    {
        $this->channelDetailAttr = [];
    }

    /**
     * 获取仓库价格数组(详情id)
     */
    protected function getWarehousePriceAttr($detailId)
    {
        $ope = app(WarehousePriceOpe::class);
        //根据详情id查询数据(详情id)
        $priceAttr = $ope->selectDataByDetailId($detailId);
        return $priceAttr;
    }

}